package com.example.administrator.materialdesign;

import android.icu.text.DateFormat;
import android.icu.text.SimpleDateFormat;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v7.widget.ButtonBarLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Date;


/**
 * Created by Administrator on 2017/5/1.
 */

public class towfragment extends Fragment {

    int a=1;
    public static final int UPDATE_TEXT=1;
    public static final int UPDATE_TEXTTIME=2;
     TextView textView;
    TextView textView1;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.tow_fragment,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        Button button=(Button) getActivity().findViewById(R.id.tow_fragment_button);
        Button button1=(Button) getActivity().findViewById(R.id.tow_fragment_button1);
        final Button button2=(Button) getActivity().findViewById(R.id.tow_fragment_button2);
         textView=(TextView)getActivity().findViewById(R.id.tow_fragment_TextView);
        textView1=(TextView)getActivity().findViewById(R.id.tow_fragment_TextView1);
//        button.setOnClickListener(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开启一个子线程，更行textView   测试，，，会报错
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("nice to meet you!");
                    }
                }).start();
            }
        });


        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message message=new Message();
                        message.what=UPDATE_TEXT;
                        handler.sendMessage(message);
                    }
                }).start();
            }
        });


        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        do {
                            try {
                                Thread.sleep(1000);
                                Message msg = new Message();
                                msg.what = UPDATE_TEXTTIME;  //消息(一个整型值)
                                handler.sendMessage(msg);// 每隔1秒发送一个msg给mHandler
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } while (true);
                    }
                }).start();

                //设置按钮点击以后不能再点击
                button2.setEnabled(false);
            }
        });
    }

    private Handler handler=new Handler(){
        public  void handleMessage(Message msg){
            switch (msg.what){
                case UPDATE_TEXT:
                    textView.setText("你好，我是子线程异步更行");
                    break;
                case UPDATE_TEXTTIME:
                    long sysTime = System.currentTimeMillis();
                    textView1.setText(String.valueOf(sysTime));
                    break;
                default:
                    break;
            }
        }
    };




}





